package com.imddy.tweb.controller;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
@RequestMapping("/filedownload")
public class FileDownloadController {
	
	private static Log log = LogFactory.getLog(FileDownloadController.class);
	
	public static void download(String fileName, String filePath, HttpServletRequest request, HttpServletResponse response) throws Exception {
		if (fileName != null) {
			File file = new File(filePath, fileName);
			System.out.println("file路径:" + file);
			if (file.exists()) {
				// 设置强制下载不打开,也有设置成response.setContentType("application/octet-stream charset=utf-8");
				response.setContentType("application/force-download charset=utf-8");
				// 设置文件名
                response.addHeader("Content-Disposition","attachment;fileName=" + new String(fileName.getBytes("UTF-8"), "iso8859-1"));
                response.setContentLength((int) file.length());
                byte[] buffer = new byte[1024];  
                FileInputStream fis = null;  
                BufferedInputStream bis = null;
                
                try {
					fis = new FileInputStream(file);
					bis = new BufferedInputStream(fis);
					OutputStream os = response.getOutputStream();
					int i = bis.read(buffer);
					while (i != -1) {
					    os.write(buffer, 0, i);
					    i = bis.read(buffer);
					}
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} finally {
					if (bis != null) {
                        try {
                            bis.close();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();  
                        }
                    }
                    if (fis != null) {
                        try {
                            fis.close();
                        } catch (IOException e) {
                            // TODO Auto-generated catch block
                            e.printStackTrace();
                        }
                    }
				}
                
			} else {
				System.out.println("文件不存在");
			}
		} else {
			System.out.println("下载的文件名为空");
		}
		
	}
	
	
	@RequestMapping("down")
	public void down(String fileName, HttpServletRequest request, HttpServletResponse response) throws Exception {
		request.setCharacterEncoding("UTF-8");
		String filePath = request.getSession().getServletContext().getRealPath("/") + "upload/";
		System.out.println("fileName: " + fileName);
		System.out.println("filePath: " + filePath);
		log.info("fileName: " + fileName);
		log.info("filePath: " + filePath);
		download(fileName, filePath, request, response);
	}

}
